
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"
	src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBXjLdTmaYkzOIa-uIHnlWCCEm2MCCiMPg&sensor=true">
</script>
<script>
	var peternakanIconBase = '<?= base_url().'img/misc/'; ?>';
	function initialize(){
		var mapProp = {
			center: new google.maps.LatLng(-7.28755, 112.733),
			zoom: 9,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("gmap"), mapProp);
		allMaps.push(map);			
		<?php
			//var_dump($peternakan->result_array());
			if($peternakan != null){
				foreach($peternakan->result_array() as $titik){
					echo "var marker".$titik['ID_TITIK_PASOK']." = new google.maps.Marker();";
					echo "marker".$titik['ID_TITIK_PASOK'].".setPosition(new google.maps.LatLng(".$titik['LAT'].", ".$titik['LON']."));";
					echo "marker".$titik['ID_TITIK_PASOK'].".setMap(map);";
					echo "marker".$titik['ID_TITIK_PASOK'].".setIcon(peternakanIconBase + 'peternakan.png');";
						
					echo "var content".$titik['ID_TITIK_PASOK']." = 'Nama: ".$titik['NAMA']." <br>Alamat: ".$titik['ALAMAT']." <br>Jumlah Sapi: ".$titik['JUMLAH_SAPI']."';";
					echo "var infoWindow".$titik['ID_TITIK_PASOK']." = new google.maps.InfoWindow({content: content".$titik['ID_TITIK_PASOK']."});";
						
					echo "onSpotClicked(marker".$titik['ID_TITIK_PASOK'].", content".$titik['ID_TITIK_PASOK'].", infoWindow".$titik['ID_TITIK_PASOK'].", map);";
						
					echo "allMarkers[".$titik['ID_TITIK_PASOK']."] = marker".$titik['ID_TITIK_PASOK'].";";
				}
			}		
		?>				
	}
	function onSpotClicked(marker, content, infowindow, map){
		google.maps.event.addListener(marker, "mouseover", function () {
			infowindow.setContent(content);
			infowindow.open(map,this);
		});		
		google.maps.event.addListener(marker, "mouseout", function () {
			infowindow.close();
		});
	}
	var allMarkers = [];
	function initialize2(){
		var mapProp = {
			center: new google.maps.LatLng(-7.28755, 112.733),
			zoom: 9,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("gmap2"),mapProp);
		allMaps.push(map);			
		google.maps.event.addListener(map, 'click', 
			function(e) {
				if(markers[0] == null){
					placeMarker(e.latLng, map);
				} else {
					markers[0].setPosition(e.latLng);
					markers[0].setMap(map);
				}
						
				$('#lat').val(e.latLng.lat());
				$('#lng').val(e.latLng.lng());
			});		
	}
				
	function placeMarker(position, map) {
		var marker = new google.maps.Marker({
			position: position,
			map: map
		});
		map.panTo(position);
		marker.setIcon(peternakanIconBase + 'peternakan.png');
		markers.push(marker);
		map.setCenter(marker.getPosition());
	}
	/*edit list data peternakan*/
	function editList(id, submit){
		$('body').animate({scrollTop:410}); 
		if(submit == 'false'){
			$('#action-title').html("Edit Data Peternakan");
			$('#nama').val($('.nama'+id).html());
			$('#alamat').val($('.alamat'+id).html());
			//$('#nama-kota').val($('.id_kota'+id).val());
			$('#no-telp').val($('.no-telp'+id).html());
			$('#email').val($('.email'+id).html());
			$('#kode-pos').val($('.kode-pos'+id).html());
			$('#tgl-berdiri').val($('.tgl-berdiri'+id).html());
			$('#jumlah-sapi').val($('.jumlah-sapi'+id).html());
			$('#info').val($('.info'+id).html());
			$('#lat').val($('.lat'+id).val());
			$('#lng').val($('.lng'+id).val());
							
			$('#button-panel').attr('onclick','editList('+id+', true)');
			$('#form-edit-peternakan').attr("action","<?php echo base_url(); ?>peternakan/update_peternakan");
			$('#id-titik-pasok').val(id);
			if(markers[0] == null){
				placeMarker(new google.maps.LatLng($('.lat'+id).val(), $('.lng'+id).val()), allMaps[1]);
			} else {
				markers[0].setPosition(new google.maps.LatLng($('.lat'+id).val(), $('.lng'+id).val()));
				markers[0].setMap(allMaps[1]);
				allMaps[1].setCenter(markers[0].getPosition());
			}				
		}
	}//edit list data peternakan
	
	function deleteList(id){
		$.ajax({
			url: "<?= base_url(); ?>peternakan/delete_peternakan",
			type: "POST",
			data: "id_titik_pasok="+id,
			success: function(msg){
				if(msg=="true") window.location = "<?= base_url(); ?>peternakan";
			}
		});
	}
	
	function resetForm(){
		$('#action-title').html("Menambahkan Data Peternakan");
		$('#form-edit-peternakan').attr("action","<?php echo base_url(); ?>peternakan/tambah_peternakan");
		$('#id-titik-pasok').val("");
		$('#lat').val("");
		$('#lng').val("");
		markers[0].setMap(null);
	}
	
	var markers = [];
	var allMaps = [];
	google.maps.event.addDomListener(window, 'load', initialize);
	google.maps.event.addDomListener(window, 'load', initialize2);
				
</script>
<div id="gmap" style="width:100%;height:380px;"></div>